﻿using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class UserBUS
    {
        public ResponseMessage AddUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserDAL da = new UserDAL();
            if (userInfo.LoginName.Length > 20 || userInfo.LoginPwd.Length > 20)
            {
                message.errcode = 1;
                message.errmsg = "新增失败，用户名和密码长度超过20字符";
            }
            else
            {
                int exist = da.CheckUser(userInfo.LoginName);
                if (exist > 0)
                {
                    message.errcode = 1;
                    message.errmsg = "账户已存在";
                }
                else
                {
                    bool result = da.AddUser(userInfo);
                    if (result)
                    {
                        message.errcode = 0;
                    }
                    else
                    {
                        message.errcode = 1;
                        message.errmsg = "新增用户失败";
                    }
                }
            }

            return message;
        }

        public ResponseMessage EditUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserDAL da = new UserDAL();
            if (userInfo.LoginName.Length > 20 || userInfo.LoginPwd.Length > 20)
            {
                message.errcode = 1;
                message.errmsg = "修改失败，用户名和密码长度超过20字符";
            }
            else
            {
                bool result = da.UpdateUser(userInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改用户信息失败";
                }
            }

            return message;
        }

        public ResponseMessage DeleteUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserDAL da = new UserDAL();
            bool result = da.DelUser(userInfo);
            if (result)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "删除用户失败";
            }

            return message;
        }

        public ResponseMessage QueryUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserDAL da = new UserDAL();
            message.errcode = 0;
            message.data = da.QueryUserList(userInfo);
            message.total = da.QueryUserListCount(userInfo);
            /*if (message.total==0)
            {
                message.errcode = 1;
                message.errmsg = "没有符合条件的记录";
            }*/
            return message;
        }

        public ResponseMessage LoadUser(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserDAL da = new UserDAL();
            message.errcode = 0;
            message.data = da.LoadUser(userInfo.ID);

            return message;
        }

        public ResponseMessage ModifyPwd(User userInfo)
        {
            ResponseMessage message = new ResponseMessage();
            UserDAL da = new UserDAL();
            bool result = da.ModifyPwd(userInfo);
            if (result)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "修改密码失败";
            }

            return message;
        }

        public ResponseMessage GetFoption(int proid)
        {
            ResponseMessage message = new ResponseMessage();
            UserDAL da = new UserDAL();
            List<Menu> list = da.GetMenu(proid);
            List<Menu> list_parent = da.GetParentMenu(proid);
            List<MenuFoption> menuFoptions = new List<MenuFoption>();
            foreach (var item in list_parent)
            {
                MenuFoption menuFoption = new MenuFoption();
                menuFoption.FunctionName = item.MenuName;
                List<Menu> list_child = new List<Menu>();
                list_child = list.FindAll(delegate (Menu e) { return e.ParentID == item.ID; });
                List<ChildMenuFoption> childMenus = new List<ChildMenuFoption>();
                foreach (var item2 in list_child)
                {
                    ChildMenuFoption childMenuFoption = new ChildMenuFoption();
                    childMenuFoption.FunctionName = item2.MenuName;
                    childMenuFoption.Url = item2.URL;
                    childMenuFoption.child_list = da.GetFOptions(item2.ID);
                    if (childMenuFoption.child_list.Count>0)
                    {
                        childMenus.Add(childMenuFoption);
                    }
                }
                menuFoption.child_list = childMenus;
                if (menuFoption.child_list.Count>0)
                {
                    menuFoptions.Add(menuFoption);
                }
            }

            message.errcode = 0;
            message.data = menuFoptions;
            return message;
        }
    }
}
